//
//  VisitorViewController.swift
//  WeiBo
//
//  Created by 姜伦 on 17/1/13.
//  Copyright © 2017年 DjangoGG. All rights reserved.
//

import UIKit

class VisitorView: UIView {
    
    
    /// 设置访客视图信息
    ///
    /// - parameter imageName: 图片名, 首页设nil
    /// - parameter message:   文字
    func setupInfo(imageName: String?, message: String) {
        
        messageLabel.text = message
        
        guard let imageName = imageName else {
            startAnimation()
            return
        }
        
        iconView.image = UIImage(named: imageName)
        homeIconView.isHidden = true
        sendSubview(toBack: shadeView)
    }
    
    // ---------------- 配套出现 ---------------
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // ---------------- 配套出现 ---------------
    
    /// 开始动画
    private func startAnimation() {
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = M_PI * 2
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        anim.isRemovedOnCompletion = false // <<<< 这句不加切换页面会导致动画停止
        
        iconView.layer.add(anim, forKey: nil)
    }
    
    // MARK: - 子控件
    private let iconView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
    private let homeIconView = UIImageView(imageName: "visitordiscover_feed_image_house")
    private let shadeView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
    private let messageLabel = UILabel(title: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜")
    
    let registerButon = UIButton(withTitle: "注册", fontSize: 15, color: UIColor.orange, backImageName: "common_button_white_disable")
    let loginButton = UIButton(withTitle: "登录", fontSize: 15, color: UIColor.darkGray, backImageName: "common_button_white_disable")
    
    /// 搭建UI
    private func setupUI() {
        
        // 灰度色 R = G = B
        backgroundColor = UIColor(white: 237.0/255.0, alpha: 1.0)
        
        addSubview(iconView
        )
        addSubview(shadeView)
        addSubview(homeIconView
        )
        addSubview(messageLabel)
        addSubview(loginButton)
        addSubview(registerButon)
        
        // -------------- 这句话注意 --------------
        for subview in subviews {
            subview.translatesAutoresizingMaskIntoConstraints = false
        }
        // -------------- 这句话注意 --------------
        
        // 1> 图标
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1.0, constant: -60))
        // 2> 小房子
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: .centerX, relatedBy: .equal, toItem: iconView, attribute: .centerX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: .centerY, relatedBy: .equal, toItem: iconView, attribute: .centerY, multiplier: 1.0, constant: 0))
        // 3> 消息文字
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .centerX, relatedBy: .equal, toItem: iconView, attribute: .centerX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .top, relatedBy: .equal, toItem: iconView, attribute: .bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 224))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 36))
        // 4> 注册按钮
        addConstraint(NSLayoutConstraint(item: registerButon, attribute: .left, relatedBy: .equal, toItem: messageLabel, attribute: .left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButon, attribute: .top, relatedBy: .equal, toItem: messageLabel, attribute: .bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButon, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButon, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 36))
        // 5> 登录按钮
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .right, relatedBy: .equal, toItem: messageLabel, attribute: .right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .top, relatedBy: .equal, toItem: messageLabel, attribute: .bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 36))
        
        // 6. 遮罩图像
        /**
         VFL : 可视化格式语言
         
         H 水平方向
         V 垂直方向
         | 边界
         [] 包装控件
         views: 是一个字典 [名字: 控件名] - VFL 字符串中表示控件的字符串
         metrics: 是一个字典 [名字: NSNumber] - VFL 字符串中表示某一个数值
         */
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[mask]-0-|", options: [], metrics: nil, views: ["mask": shadeView]))
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[mask]-(btnHeight)-[regButton]", options: [], metrics: ["btnHeight": -36], views: ["mask": shadeView, "regButton": registerButon]))
    }
    
    
}

